package com.train.course.controller;

import com.train.common.annotation.Anonymous;
import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.page.TableDataInfo;
import com.train.course.domain.VideoAction;
import com.train.course.service.IVideoActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分解动作视频 控制器
 */
@RestController
@RequestMapping("/course/action")
public class VideoActionController extends BaseController {
    @Autowired
    private IVideoActionService videoActionService;

    /**
     * 查询分解动作视频列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(VideoAction videoAction) {
        startPage();
        List<VideoAction> list = videoActionService.selectVideoActionList(videoAction);
        return getDataTable(list);
    }

    /**
     * 根据视频集ID查询分解动作视频列表
     */
    @Anonymous
    @GetMapping("/listByEpisode/{episodeId}")
    public AjaxResult listByEpisode(@PathVariable Long episodeId) {
        List<VideoAction> list = videoActionService.selectVideoActionListByEpisodeId(episodeId);
        return AjaxResult.success(list);
    }

    /**
     * 获取分解动作视频详细信息
     */
    @Anonymous
    @GetMapping(value = "/{actionId}")
    public AjaxResult getInfo(@PathVariable("actionId") Long actionId) {
        return AjaxResult.success(videoActionService.selectVideoActionById(actionId));
    }

    /**
     * 新增分解动作视频
     */
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody VideoAction videoAction) {
        int rows = videoActionService.insertVideoAction(videoAction);
        if (rows > 0) {
            // MyBatis 会将生成的 actionId 回填到 videoAction 对象中
            Map<String, Object> data = new HashMap<>();
            data.put("actionId", videoAction.getActionId());
            return AjaxResult.success("操作成功", data);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改分解动作视频
     */
    @Anonymous
    @PutMapping
    public AjaxResult edit(@RequestBody VideoAction videoAction) {
        return toAjax(videoActionService.updateVideoAction(videoAction));
    }

    /**
     * 删除分解动作视频
     */
    @Anonymous
    @DeleteMapping("/{actionIds}")
    public AjaxResult remove(@PathVariable Long[] actionIds) {
        return toAjax(videoActionService.deleteVideoActionByIds(actionIds));
    }
}